package com.fh.wanandroid.ui.knowledge.presenter;

import android.util.Log;

import com.fh.wanandroid.base.BaseResponse;
import com.fh.wanandroid.net.RetrofitHelper;
import com.fh.wanandroid.ui.knowledge.KnowledgeDataList;
import com.fh.wanandroid.ui.knowledge.contract.KnowledgeListContract;
import com.fh.wanandroid.ui.mainpager.bean.FeedArticleListData;
import com.fh.wanandroid.utils.NetErrorUtil;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class KnowledgeListPresenter extends  KnowledgeListContract.Presenter {

    int page;
    @Override
  public  void loadInitData(int cid) {

        page = 0;
        mCompositeDisposable.add(RetrofitHelper.createArticleApi().getKnowledgedArticleList(page,cid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<FeedArticleListData>() {
                    @Override
                    public void accept(FeedArticleListData feedArticleListData) throws Exception {

                        mView.refreshKnowledgeArticle(feedArticleListData);
//                        mView.refreshKnowledge(knowledgeDatas.getData());
//                        mView.appendData(girlBean.getResults());
//                        mView.refreshArticle(articleBean);
                        Log.e("KnowledgeListPresenter","------------------KnowledgeListPresenter loadInitData feedArticleListData:"+feedArticleListData.getData().getCurPage());
                        Log.e("KnowledgeListPresenter","------------------KnowledgeListPresenter loadInitData:"+feedArticleListData.getData().getDatas());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable t) throws Exception {
//                        mView.loadArticlesFail(NetErrorUtil.handleThrowable(t));
                        mView.loadDataFail(NetErrorUtil.handleThrowable(t));
                    }
                }));

    }

    @Override
    public void loadMoreData(int cid) {

        mCompositeDisposable.add(RetrofitHelper.createArticleApi().getKnowledgedArticleList(++page,cid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<FeedArticleListData>() {
                    @Override
                    public void accept(FeedArticleListData articleBean) throws Exception {
                        mView.appendKnowledgeArticle(articleBean);
                        Log.e("KnowledgeListPresenter","------------------KnowledgeListPresenter loadMoreData :"+articleBean.getData().toString());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable t) throws Exception {
//                        mView.loadArticlesFail(NetErrorUtil.handleThrowable(t));
                        mView.loadDataFail(NetErrorUtil.handleThrowable(t));
                    }
                }));
    }
}
